using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class AppearEffect : MonoBehaviour
{
    public Image targetImage;             // 要渐变透明度的 Image
    public GameObject scaleTarget;        // 要缩放的目标对象
    public float fadeDuration = 1f;       // 透明度渐变时间
    public float scaleDuration = 1f;      // 缩放插值时间

    private void OnEnable()
    {
        StartCoroutine(PlayEffect());
    }

    private IEnumerator PlayEffect()
    {
        // 暂停时间
        Time.timeScale = 0;

        // ---------------- 渐变透明度 ----------------
        Color color = targetImage.color;
        color.a = 0f;
        targetImage.color = color;

        float timer = 0f;
        while (timer < fadeDuration)
        {
            timer += Time.unscaledDeltaTime; // 注意使用 unscaledDeltaTime
            float alpha = Mathf.Lerp(0f, 200f / 255f, timer / fadeDuration);
            color.a = alpha;
            targetImage.color = color;
            yield return null;
        }

        // 确保结束时透明度为 200
        color.a = 200f / 255f;
        targetImage.color = color;

        // ---------------- 缩放目标对象 ----------------
        scaleTarget.transform.localScale = Vector3.zero;

        timer = 0f;
        while (timer < scaleDuration)
        {
            timer += Time.unscaledDeltaTime;
            float scaleValue = Mathf.Lerp(0f, 1f, timer / scaleDuration);
            scaleTarget.transform.localScale = new Vector3(scaleValue, scaleValue, scaleValue);
            yield return null;
        }

        // 确保最终缩放为 1
        scaleTarget.transform.localScale = Vector3.one;

    }
    // 重新加载当前场景
    public void ReloadScene()
    {
        Time.timeScale = 1; // 确保恢复时间
        Scene currentScene = SceneManager.GetActiveScene();
        SceneManager.LoadScene(currentScene.name);
    }
}
